package cn.kgc.javaweb.dao.impl;

import cn.hutool.json.JSONUtil;
import cn.kgc.javaweb.dao.RepairDao;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.Repair;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 涛涛
 * @version 1.0
 */
public class RepairDaoImpl implements RepairDao {
    @Override
    public List<Repair> selectPage(UserPageDTO userPageDTO) {
        Integer page = userPageDTO.getPage();
        Integer limit = userPageDTO.getLimit();

       Integer start = (page-1)*limit;

        String searchParams = userPageDTO.getSearchParams();

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql ="select * from repair where 1=1";
        ArrayList<Object> params = new ArrayList<>();

        if (searchParams!=null){

            Repair repair = JSONUtil.toBean(searchParams, Repair.class);
            System.out.println(repair+"fdfd");
            Boolean state = repair.getState();

            params.add(state);
            sql+=" and state = ?";

        }
        sql+=" limit ?,?";
        params.add(start);
        params.add(limit);

        try {
            List<Repair> repairs = queryRunner.query(sql, new BeanListHandler<Repair>(Repair.class), params.toArray());
            return repairs;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new  RuntimeException("分页查询失败");

        }



    }

    @Override
    public Long selectTotal(UserPageDTO userPageDTO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String searchParams = userPageDTO.getSearchParams();

        String sql ="select count(*) from repair where 1=1";
        ArrayList<Object> params = new ArrayList<>();

        if (searchParams!=null){

            Repair repair = JSONUtil.toBean(searchParams, Repair.class);
            Boolean state = repair.getState();
            sql+=" and state = ?";
            params.add(state);

        }

        try {
            Long total = queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray());
            return total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException("查询总条数失败");
        }



    }

    @Override
    public void state(boolean checked, Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql ="update repair set state=? where id =?";
        try {
            int update = queryRunner.update(sql, checked, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    public void update(Repair repair) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update repair set content = ? ,result=? ,state = ? where id = ?";
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String ft = simpleDateFormat.format(repair.getFtime());

        try {
            queryRunner.update(sql, repair.getContent(), repair.getResult(),repair.getState(), repair.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(CodeMsg.UPDATE_FAIL.getMsg());
        }

    }
    @Override
    public void add(Repair repair) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into repair(content,ftime,result,state) values(?,?,?,?)";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String ftime = simpleDateFormat.format(repair.getFtime());
        try {
            int update = queryRunner.update(sql, repair.getContent(),  ftime, repair.getResult(), repair.getState());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(CodeMsg.ADD_FAIL.getMsg());
        }
    }

    @Override
    public void delete(String ids) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from repair where id = ?";

        String[] split = ids.split(",");
        ArrayList<Integer> allId = new ArrayList<>();
        for (String s : split) {
            allId.add(Integer.valueOf(s));
        }

        allId.forEach(id -> {
            try {
                System.out.println(id);
                queryRunner.update(sql, id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                throw new RuntimeException(CodeMsg.DELETE_FAIL.getMsg());
            }
        });
    }














}
